<template>
<div class="login_box">
  <UiCard class="login_card">
    <h1 class="title">小义博客</h1>
    <h2 class="subtitle">后台管理系统</h2>
    <el-form label-width="60px" class="form" >
      <el-form-item label="用户名">
        <el-input v-model="uname"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="pwd"></el-input>
      </el-form-item>
    </el-form>
    <el-button @click="login" type="primary">登录</el-button>
  </UiCard>
</div>
</template>

<script setup>
import UiCard from "../../components/ui/UiCard.vue";
import {ref} from "vue";
import {request} from "../../api/baseFentch";
import {useRoute, useRouter} from "vue-router";

const route = useRoute()
const router = useRouter()

const uname = ref('')
const pwd = ref('')

const login = async () => {
  let f = new FormData()
  f.append('uname', uname.value)
  f.append('pwd', pwd.value)
  let data = await request('/auth', 'POST', f)
  console.log(data);
  if (data.code === 0) {
    window.localStorage.setItem("token", data.object)
    let refer = route.query?.refer
    if (refer !== undefined) {
      await router.push(refer)
      return
    }
    await router.push("/admin")
  }
}
</script>

<style scoped>
.login_box {
  width: 100vw;
  height: 100vh;
  background-color: #f2f2f2;
  display: flex;
  justify-content: center;
  place-items: center;
}

.login_card {
  display: flex;
  flex-direction: column;
  place-items: center;
}

.form {
  width: 300px;
}

.title {
  margin: 0;
  padding: 0;
}

.subtitle {
  margin-top: 15px;
  margin-bottom: 20px;
  color: #999999;
}
</style>